package org.third.integration.kubernates.cmd.imp;

import org.apache.http.client.methods.HttpDelete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.third.integration.kubernates.cmd.KubectlDelete;
import org.third.integration.kubernates.cmd.imp.api.K8sApiBuilder;
import org.third.integration.kubernates.cmd.imp.bean.KubeInstanceInfo;
import org.third.integration.kubernates.cmd.imp.client.KubeRquestClient;
import org.third.integration.kubernates.cmd.imp.client.bean.ApiServerResponse;
import org.third.integration.kubernates.cmd.imp.exp.NotValidK8sYamlException;
import org.third.integration.kubernates.cmd.imp.exp.RequestApiServerException;
import org.third.integration.kubernates.cmd.imp.utils.K8S_TYPE_UTILS;

@Component
public class KubeDeleteImp implements KubectlDelete{
	
	@Autowired
	private K8sApiBuilder k8sApiBuilder;
	
	@Autowired
	KubeRquestClient kubeRquestClient;

	@Override
	public KubeInstanceInfo execute(String type, String apiVersion,
			String namespace, String instanceName) throws NotValidK8sYamlException, RequestApiServerException {
		
		KubeInstanceInfo kubeInstanceInfo = validateInput(type, apiVersion, namespace, instanceName);
		
		requestK8s(kubeInstanceInfo);
		
		return kubeInstanceInfo;
	}

	private KubeInstanceInfo validateInput(String type, String apiVersion,
			String namespace, String instanceName)
			throws NotValidK8sYamlException {
		if(!K8S_TYPE_UTILS.getSpecificTypes().contains(type) && StringUtils.isEmpty(namespace)){
			throw new RuntimeException("namespace can't be empty ");
		}
		if(StringUtils.isEmpty(instanceName) || StringUtils.isEmpty(apiVersion) || StringUtils.isEmpty(type)){
			throw new RuntimeException("type or apiVersion or name can't be empty");
		}
		KubeInstanceInfo kubeInstanceInfo = new KubeInstanceInfo(type, apiVersion, instanceName, namespace, null);
		return kubeInstanceInfo;
	}
	
	private void requestK8s(KubeInstanceInfo kubeInstanceInfo) throws RequestApiServerException {
		String url = k8sApiBuilder.buildAPI(kubeInstanceInfo.getNamespace(), kubeInstanceInfo.getApiVersion(), kubeInstanceInfo.getType(), kubeInstanceInfo.getInstanceName());
		HttpDelete httpDelete = new HttpDelete(url);		
		ApiServerResponse apiServerResponse = kubeRquestClient.requestK8s(httpDelete);
		kubeInstanceInfo.setStatusCode(apiServerResponse.getStatusCode());
		kubeInstanceInfo.setResponseBody(apiServerResponse.getResponseBody());
		if(apiServerResponse.getException() != null){
			throw new RequestApiServerException("Failed to request API server", apiServerResponse.getException());
		}
	}
	
	
	@Override
	public KubeInstanceInfo executeListWithQueryParam(String type, String apiVersion,
			String namespace, String instanceName) throws NotValidK8sYamlException, RequestApiServerException {
		
		KubeInstanceInfo kubeInstanceInfo = validateInput(type, apiVersion, namespace, instanceName);
		
		requestK8sListWithQueryParam(kubeInstanceInfo);
		
		return kubeInstanceInfo;
	}
	
	private void requestK8sListWithQueryParam(KubeInstanceInfo kubeInstanceInfo) throws RequestApiServerException {
		String url = k8sApiBuilder.buildListApiWithQueryParam(kubeInstanceInfo.getNamespace(),kubeInstanceInfo.getApiVersion(),kubeInstanceInfo.getType(), kubeInstanceInfo.getInstanceName());
		System.out.println("url:"+url);
		HttpDelete httpDelete = new HttpDelete(url);		
		ApiServerResponse apiServerResponse = kubeRquestClient.requestK8s(httpDelete);
		kubeInstanceInfo.setStatusCode(apiServerResponse.getStatusCode());
		kubeInstanceInfo.setResponseBody(apiServerResponse.getResponseBody());
		if(apiServerResponse.getException() != null){
			throw new RequestApiServerException("Failed to request API server", apiServerResponse.getException());
		}
	}


}
